package cn.antia.cep.page;

import java.io.*;
import java.util.*;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.*;

import cn.antia.cep.common.*;
import cn.antia.cep.page.impl.MztreeTreePrinter;
import cn.antia.cep.right.*;

public class AntiaTreeTag extends SimpleTagSupport {
//	树的类型，如果不设置，默认为mztree
    private String treeType; 
   //操作方式，有两个选项。1是disp(显示)2是mana(维护) 
    private String operate;
    //角色列表
    private List roles;

	public void setTreeType(String treeType) {
        this.treeType = treeType;
    }
    
    /**
	 * @param roles the roles to set
	 */
	public void setRoles(List roles) {
		this.roles = roles;
	}

	/**
	 * @param operate the operate to set
	 */
	public void setOperate(String operate) {
		this.operate = operate;
	}
	
	public void doTag() throws JspException, IOException {
		TreePrinter treePrinter = null; //树的输出类
		RightTree rightTree = null;
        try {
            JspWriter out = getJspContext().getOut();
            if(treeType == null || treeType.trim().length() < 1){
            	treeType = "mztree";
            }
            if(treeType != null && treeType.equalsIgnoreCase("mztree")){
                treePrinter = new MztreeTreePrinter();
            }
            if(operate == null || operate.trim().length() < 1){
            	AntiaLog.error("打印资源树树没有指定打印方法。");
            	return;
            }
            //如果是要维护资源树并且没有权限的话，取出所有的有效资源
            ResourceTreeBuilder builder = (ResourceTreeBuilder)ContextBean.getBean("resourceTreeBuilder");
            //如果指定了角色集，则根据角色查询出资源
            if(roles != null){
            	rightTree = builder.buildResourceTree(roles);
            //如果没有指定角色，并且是维护操作，则查询出所有的资源
            }else{
            	AntiaLog.error("没有取到任何权限。");
            }
            treePrinter.recursivePrint(rightTree,out,operate);
            out.flush();
        } catch (Exception e){
        	e.printStackTrace();
            AntiaLog.error("打印资源树时资源树时发生异常。 :" + e.getLocalizedMessage());
        }
    }
}
